package com.ql.gamelogm.dao.impl;

import com.mysql.cj.xdevapi.Collection;
import com.ql.gamelogm.dao.MailDao;
import com.ql.gamelogm.entities.Log;
import com.ql.gamelogm.entities.Mail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;

@Repository
public class MailDaoImpl implements MailDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public void SaveMailSended(Mail mail) {
        String sql = "insert into mailsended(state,sendto,subject,content,sendfrom,date) values(?,?,?,?,?,?)";
        jdbcTemplate.update(sql,
                mail.getState(),
                mail.getSendTo(),
                mail.getSubject(),
                mail.getContent(),
                mail.getSendFrom(),
                mail.getDate()
        );
    }

    @Override
    public void SaveMailInBox(Mail mail) {
        String sql = "insert into mailinbox(state,sendto,subject,content,sendfrom,date) values(?,?,?,?,?,?)";
        jdbcTemplate.update(sql,
                mail.getState(),
                mail.getSendTo(),
                mail.getSubject(),
                mail.getContent(),
                mail.getSendFrom(),
                mail.getDate()
        );
    }

    @Override
    public boolean SaveMailDeleted(Mail mail) {
        String sql = "insert into maildeleted(state,sendto,subject,content,sendfrom,date) values(?,?,?,?,?,?)";
        int update = jdbcTemplate.update(sql,
                mail.getState(),
                mail.getSendTo(),
                mail.getSubject(),
                mail.getContent(),
                mail.getSendFrom(),
                mail.getDate()
        );
        if(update==0){
            return false;
        }
        return true;

    }

    @Override
    public int DeleteMailInBoxById(int id) {
        String sql = "delete from mailinbox where id="+id;
        jdbcTemplate.update(sql);
        List<Mail> allMailsDeleted = findAllMailsDeleted();
        return allMailsDeleted.size();
    }

    @Override
    public int DeleteMailSendedById(int id) {
        String sql = "delete from mailsended where id="+id;
        jdbcTemplate.update(sql);
        List<Mail> allMailsDeleted = findAllMailsDeleted();
        return allMailsDeleted.size();
    }


    @Override
    public List<Mail> findAllMailsInBox() {
        String sql = "select * from mailinbox";
        List<Mail> mailInBoxList = jdbcTemplate.query(sql, (resultSet, rowNum) -> new Mail(
                resultSet.getInt("id"),
                resultSet.getString("state"),
                resultSet.getString("sendto"),
                resultSet.getString("subject"),
                resultSet.getString("content"),
                resultSet.getString("sendfrom"),
                resultSet.getTimestamp("date")
        ));
        return mailInBoxList;
    }

    @Override
    public List<Mail> findAllMailsSended() {
        String sql = "select * from mailsended";
        List<Mail> mailSendedList = jdbcTemplate.query(sql, (resultSet, rowNum) -> new Mail(
                resultSet.getInt("id"),
                resultSet.getString("state"),
                resultSet.getString("sendto"),
                resultSet.getString("subject"),
                resultSet.getString("content"),
                resultSet.getString("sendfrom"),
                resultSet.getTimestamp("date")
        ));
        return mailSendedList;
    }

    @Override
    public List<Mail> findAllMailsInBoxBySendTo(String sendto) {
        String sql="select * from mailinbox where sendto='"+sendto+"'";
        List<Mail> mailInBoxList = jdbcTemplate.query(sql, (resultSet, rowNum) -> new Mail(
                resultSet.getInt("id"),
                resultSet.getString("state"),
                resultSet.getString("sendto"),
                resultSet.getString("subject"),
                resultSet.getString("content"),
                resultSet.getString("sendfrom"),
                resultSet.getTimestamp("date")
        ));
        return mailInBoxList;
    }

    @Override
    public List<Mail> findAllMailsDeletedBySendTo(String sendto) {
        String sql="select * from maildeleted where sendto='"+sendto+"'";
        List<Mail> mailDeletedList = jdbcTemplate.query(sql, (resultSet, rowNum) -> new Mail(
                resultSet.getInt("id"),
                resultSet.getString("state"),
                resultSet.getString("sendto"),
                resultSet.getString("subject"),
                resultSet.getString("content"),
                resultSet.getString("sendfrom"),
                resultSet.getTimestamp("date")
        ));
        return mailDeletedList;
    }

    @Override
    public List<Mail> findAllMailsDeleted() {
        String sql = "select * from maildeleted";
        List<Mail> mailDeletedList = jdbcTemplate.query(sql, (resultSet, rowNum) -> new Mail(
                resultSet.getInt("id"),
                resultSet.getString("state"),
                resultSet.getString("sendto"),
                resultSet.getString("subject"),
                resultSet.getString("content"),
                resultSet.getString("sendfrom"),
                resultSet.getTimestamp("date")
        ));
        return mailDeletedList;
    }

    @Override
    public void updateMailState(int id) {
        String sql="update mailinbox set state='已读' where id="+id;
        jdbcTemplate.update(sql);
    }

    @Override
    public List<Mail> findMailInBoxById( int id) {
        String sql = "select * from mailinbox where id=" + id;
        List<Mail> mailBySendToList = jdbcTemplate.query(sql, (resultSet, rowNum) -> new Mail(
                resultSet.getInt("id"),
                resultSet.getString("state"),
                resultSet.getString("sendto"),
                resultSet.getString("subject"),
                resultSet.getString("content"),
                resultSet.getString("sendfrom"),
                resultSet.getTimestamp("date")
        ));
        return mailBySendToList;
    }


    @Override
    public List<Mail> findMailSendedById( int id) {
        String sql = "select * from mailsended where  id=" + id;
        List<Mail> mailBySendToList = jdbcTemplate.query(sql, (resultSet, rowNum) -> new Mail(
                resultSet.getInt("id"),
                resultSet.getString("state"),
                resultSet.getString("sendto"),
                resultSet.getString("subject"),
                resultSet.getString("content"),
                resultSet.getString("sendfrom"),
                resultSet.getTimestamp("date")
        ));
        return mailBySendToList;
    }

    @Override
    public List<Mail> findMailDeletedById( int id) {
        String sql = "select * from maildeleted where  id=" + id;
        List<Mail> mailBySendToList = jdbcTemplate.query(sql, (resultSet, rowNum) -> new Mail(
                resultSet.getInt("id"),
                resultSet.getString("state"),
                resultSet.getString("sendto"),
                resultSet.getString("subject"),
                resultSet.getString("content"),
                resultSet.getString("sendfrom"),
                resultSet.getTimestamp("date")
        ));
        return mailBySendToList;
    }
}
